<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">

				<el-form-item>
					<el-input v-model="filters.city" placeholder="城市"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="findPageSlowCity">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="addCity">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
		<template>
			<el-table :data="citys" highlight-current-row v-loading="loading" max-height="620"  border  >
				<el-table-column type="index" width="80" label="编号">
				</el-table-column>
				<el-table-column prop="province" label="省份"  >
				</el-table-column>
				<el-table-column prop="city" show-overflow-tooltip  label="城市"  >
				</el-table-column>
				<el-table-column prop="state" show-overflow-tooltip  label="状态"   :formatter="formatStatus">
				</el-table-column>
				<el-table-column prop="remark" show-overflow-tooltip  label="备注" >
				</el-table-column>
				<el-table-column prop="createTime" show-overflow-tooltip  label="创建时间" show-overflow-tooltip >
				</el-table-column>

				<el-table-column label="操作"    >
					<template scope="scope"   >
						<el-button size="small"   @click="updateProduct( scope.row)">
							修改
						</el-button>
						<el-button size="small"   @click="updateState(scope.$index, scope.row)">
							{{scope.row.state == 1 ? '禁用' : '启用' }}
						</el-button>
					</template>
				</el-table-column>
			</el-table>
		</template>
		<el-pagination
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
				layout="total,sizes,prev, pager, next"
				:total="total">
		</el-pagination>

		<!--新增界面-->
		<el-dialog title="新增慢充地市" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="省份" prop="province">
					<el-input v-model="addForm.province" style="width: 200px" ></el-input>
				</el-form-item>
				<el-form-item label="地市" prop="city">
					<el-input v-model="addForm.city" style="width: 200px"  ></el-input>
				</el-form-item>
				<el-form-item label="备注">
					<el-input type="textarea"  style="width: 200px"  v-model="addForm.remark"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

		<el-dialog title="编辑慢充地市" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="省份" prop="province">
					<el-input v-model="editForm.province" style="width: 200px" ></el-input>
				</el-form-item>
				<el-form-item label="地市" prop="city">
					<el-input v-model="editForm.city" style="width: 200px"  ></el-input>
				</el-form-item>
				<el-form-item label="备注">
					<el-input type="textarea"  style="width: 200px"  v-model="editForm.remark"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>


</template>
<script>
    import { addSlowcity,updateSlowcity,updateStateSlow,findPageSlowCity} from '../../api/api';

    //import NProgress from 'nprogress'
    import moment from 'moment';
    var crypto = require('crypto');
    export default {
        data() {
            return {
                filters: {
                    city:'',
                },
                isShowSupper:JSON.parse(sessionStorage.getItem("user")).usertype === 2 ? true : false,
                start:0,
                pageSize:10,
                total:0,
                loading: false,
                citys:[],
                addFormVisible: false,
                addLoading:false,
                importFormLoading: false,
                importFormRules: {
                },
                addForm:{
                    province:'',
                    city:'',
                    remark:''
                },addFormRules:{

                },
                editFormVisible:false,
                editFormRules:{

                },
                editForm:{
                    province:'',
                    city:'',
                    remark:''
                }
            }
        },
        methods: {
            formatStatus: function (row, column) {
                return row.state == 1 ? '启用' : row.state == 0 ? '禁用 ' : "未知";
            },
            findPageSlowCity: function () {
                let para = {
                    start:this.start,
                    pageSize:this.pageSize,
                    city:this.filters.city,
                };
                this.loading = true;
                findPageSlowCity(para).then(datas => {
                    this.loading = false;
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.citys = data.result;
                        this.total = data.totalCount;
                    }
                }).catch(e =>{

                });
            },handleCurrentChange:function(val){
                this.start = val - 1;
                this.findPageSlowCity();
            },
            handleSizeChange:function(val){
                this.pageSize = val;
                this.findPageSlowCity();
            }
            ,updateState:function (index, row){
                this.$confirm('确认启用或禁用产品吗？', '提示', {}).then(() => {
                    let para = {
                        slowCityId: row.slowCityId,
                        state : row.state == 1 ? 0 : '1'
                    };
                    updateStateSlow(para).then((datas) => {
                        this.listLoading = false;
                        let {message, code, data} = datas;
                        if (code !== "0") {
                            this.$message({
                                message: message,
                                type: 'error'
                            });
                        }
                        this.$message({
                            message: message,
                            type: 'info'
                        });
                        this.findPageSlowCity();
                    });
                });
            },addCity(){
                this.addFormVisible = true;
            },addSubmit:function(){
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            let para = Object.assign({}, this.addForm);
                            addSlowcity(para).then((datas) => {
                                let { message, code, data } = datas;
                                if (code !== "0") {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                                this.addFormVisible = false;
                                this.findPageSlowCity();
                                this.$refs['addForm'].resetFields();
                            }).catch(function (error) {
                                console.log(error);
                            });
                        });
                    }
                });
            },updateProduct(row){
                this.editForm = Object.assign({}, row);
                this.editFormVisible = true;

            },editSubmit(){
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            let para = Object.assign({}, this.editForm);
                            updateSlowcity(para).then((datas) => {
                                let { message, code, data } = datas;
                                if (code !== "0") {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                                this.editFormVisible = false;
                                this.findPageSlowCity();
                                this.$refs['addForm'].resetFields();
                            }).catch(function (error) {
                                console.log(error);
                            });
                        });
                    }
                });
            }
        },
        mounted() {
            this.findPageSlowCity();
        },
        beforeCreate: function (){
//            window.location.reload();
        }
    };

</script>

<style scoped>

</style>
